package com.inspinia.base.service;

import com.inspinia.base.dao.BaseDao;
import com.inspinia.base.pagination.PageQueryVo;
import com.inspinia.base.pagination.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 实现层基类，提供一些通用的基本方法
 * @author veblen
 * @date 16-1-14
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected abstract BaseDao<T> getDao();

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public T get(Long id) {
        return getDao().findById(id);
    }

    @Override
    public Pager<T> findPage(PageQueryVo pageVo) {
        Pager<T> page = getDao().findPage(pageVo);
        page.setPageNo(pageVo.getPageNo());
        page.setPageSize(pageVo.getPageSize());
        return page;
    }

    @Override
    public int insert(T bean) {
        int i = getDao().insert(bean);
        if (i == 0) {
            throw new RuntimeException("创建失败");
        }
        return i;
    }

    @Override
    public int update(T bean) {
        int i = getDao().update(bean);
        if (i == 0) {
            throw new RuntimeException("修改失败");
        }
        return i;
    }

    @Override
    public int delete(Long id) {
        int i = getDao().delete(id);
        if (i == 0) {
            throw new RuntimeException("删除失败");
        }
        return i;
    }
}
